<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
 * @param {string} s
 * @param {string[]} words
 * @return {number}
 */
        var expressiveWords = function (s, words) {
            let total = 0,
                flag = true
            words.forEach(item => {
                flag = true
                for (let i = 0; i < item.length; i++) {
                    if (s[i] === item[i]) {
                        if (i === item.length - 1 && (sameLength(s, i) < 3 || item.length + sameLength(s, i) - 1 != s.length)) {
                            flag = false
                            break
                        }
                        continue
                    } else {
                        if (sameLength(s, i - 1) >= 3) {
                            let arr = Array(sameLength(s, i - 1) - 1).fill(s[i - 1])
                            let temp = item.split('')
                            temp.splice(i, 0, ...arr)
                            item = temp.join('')
                        } else {
                            flag = false
                            break
                        }
                    }
                }
                console.log(item)
                if (flag) {
                    total++
                }
            })
            console.log(total)
            return total
            function sameLength(str, idx) {
                let count = 1
                for (let i = idx + 1; i < str.length; i++) {
                    if (str[i] === str[idx]) {
                        count++
                    } else {
                        return count
                    }
                }
                return count
            }
        };
        let s = "dddiiiinnssssssoooo",
            words = ["dinnssoo", "ddinso", "ddiinnso", "ddiinnssoo", "ddiinso", "dinsoo", "ddiinsso", "dinssoo", "dinso"]

        expressiveWords(s, words)
    </script>
</body>

</html>